using System;
using System.Collections;
using System.Data;
using TSoft.Data;


namespace Coffee.Lists
{
	public class clsLST_Product : iControls.IBaseClass
	{
		#region Prop
		public System.Int32 ProductID
		{
			 get;
			 set;
		}

		public System.String Code
		{
			 get;
			 set;
		}

		public System.String Name
		{
			 get;
			 set;
		}

		public System.Int32 UnitID
		{
			 get;
			 set;
		}

		public System.Int32 Type
		{
			 get;
			 set;
		}

		public System.Int32 GroupID
		{
			 get;
			 set;
		}

		public System.Byte[] Image
		{
			 get;
			 set;
		}

		public System.String Notes
		{
			 get;
			 set;
		}

		#endregion

		#region  Constructors
        public clsLST_Product()
        { 
        }

		#endregion

		#region Methods
		public override void ResetData()
		{
			ProductID = Int32.MinValue;
			Code = string.Empty;
			Name = string.Empty;
			UnitID = int.MinValue;
			Type = 1;
			GroupID = int.MinValue;
			Image = null;
            Notes = string.Empty;
		}

		public void LoadData(System.Int32 v_ProductID)
		{
			DataTable dt = new DataTable();
			try
			{
				dt = DBLayer.ExecDataTable("SP_LST_Product", " Command,ProductID", "LoadData", v_ProductID);
				this.ResetData();
				if(dt==null || dt.Rows.Count<=0)
					return;
				this.FillData(dt.Rows[0]);
			}
			catch(Exception ex)
			{
				throw ex;
			}
		}

		public override void FillData(DataRow row)
		{
			ResetData();
			if(row.Table.Columns.Contains("ProductID"))
				ProductID = TSoft.Data.ValueType.GetValue(row["ProductID"], ProductID);
			if(row.Table.Columns.Contains("Code"))
				Code = TSoft.Data.ValueType.GetValue(row["Code"], Code);
			if(row.Table.Columns.Contains("Name"))
				Name = TSoft.Data.ValueType.GetValue(row["Name"], Name);
			if(row.Table.Columns.Contains("UnitID"))
				UnitID = TSoft.Data.ValueType.GetValue(row["UnitID"], UnitID);
			if(row.Table.Columns.Contains("Type"))
				Type = TSoft.Data.ValueType.GetValue(row["Type"], Type);
			if(row.Table.Columns.Contains("GroupID"))
				GroupID = TSoft.Data.ValueType.GetValue(row["GroupID"], GroupID);
			if(row.Table.Columns.Contains("Image"))
				Image = TSoft.Data.ValueType.GetValue(row["Image"], Image);
			if(row.Table.Columns.Contains("Notes"))
				Notes = TSoft.Data.ValueType.GetValue(row["Notes"], Notes);
		}

		public override void OnUpdate()
		{
			Hashtable output = null;
			string s_ParamName = "";
			object[] o_ParamValue = null;
			try
			{
				TSoft.Data.ParamHolder.ClearAll();
				TSoft.Data.ParamHolder.AddParam("Command", "Update");
				TSoft.Data.ParamHolder.AddParam("ProductID", ProductID);
				TSoft.Data.ParamHolder.AddParam("Code", Code);
				TSoft.Data.ParamHolder.AddParam("Name", Name);
				TSoft.Data.ParamHolder.AddParam("UnitID", UnitID);
				TSoft.Data.ParamHolder.AddParam("Type", Type);
				TSoft.Data.ParamHolder.AddParam("GroupID", GroupID);
				TSoft.Data.ParamHolder.AddParam("Image", Image);
				TSoft.Data.ParamHolder.AddParam("Notes", Notes);
				TSoft.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
				DBLayer.ExecNonQuery("SP_LST_PRODUCT", s_ParamName, ref output, o_ParamValue);
			}
			catch( Exception ex)
			{
				throw ex;
			}
		}

		public override void OnDelete()
		{
			string s_ParamName = "";
			object[] o_ParamValue = null;
			try
			{
				TSoft.Data.ParamHolder.ClearAll();
				TSoft.Data.ParamHolder.AddParam("Command", "Delete");
				TSoft.Data.ParamHolder.AddParam("ProductID", ProductID);
				TSoft.Data.ParamHolder.GetParams(ref s_ParamName,ref o_ParamValue);
				DBLayer.ExecNonQuery("USP_LST_PRODUCT", s_ParamName, o_ParamValue);
			}
			catch( Exception ex)
			{
				throw ex;
			}
		}


		#endregion

        #region Developer
        public DataTable GetAll()
        {
            try
            {               
                return DBLayer.ExecDataTable("uSP_LST_Product", "Command", "GetAll");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Update(System.Data.DataTable Data)
        {
            try
            {
                Hashtable output = null;
                DBLayer.BeginTransaction();

                foreach (DataRow row in Data.Rows)
                {
                    if (row.RowState == DataRowState.Added || row.RowState == DataRowState.Modified)
                    {
                        DBLayer.ExecNonQuery("usp_LST_Product", "Command,ProductID,Code,Name,UnitID,Type,GroupID,Notes", ref output, "Update",
                            TSoft.Cvert.ToInt32(row["ProductID"], 0), TSoft.Cvert.ToString(row["Code"], "")
                            , TSoft.Cvert.ToString(row["Name"], ""), TSoft.Cvert.ToInt32(row["UnitID"], 0)
                            , TSoft.Cvert.ToInt32(row["Type"], 0), TSoft.Cvert.ToInt32(row["GroupID"], 0)
                            , TSoft.Cvert.ToString(row["Notes"], ""));
                        if (!object.Equals(output, null) && output.Count > 0)
                            row["ProductID"] = TSoft.Cvert.ToInt32(output["ProductID"], 0);
                    }
                }

                DBLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                DBLayer.RollbackTransaction();
                throw ex;
            }
        }
        #endregion
    }
}
